package com.example.ivos.dict.service.impl;

import com.example.ivos.dict.mapper.DictMapper;
import com.example.ivos.dict.pojo.dto.DictQuery;
import com.example.ivos.dict.pojo.dto.DictSaveParam;
import com.example.ivos.dict.pojo.entity.Dict;
import com.example.ivos.dict.pojo.vo.DictVO;
import com.example.ivos.dict.service.DictService;
import com.example.ivos.dictoption.pojo.entity.DictOption;
import com.example.ivos.dictoption.service.impl.DictOptionServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictOptionServiceImpl dictOptionService;

    public List<DictVO> selectDict(DictQuery dictQuery) {
        log.debug("查询字典信息:dictQuery={}", dictQuery);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictQuery, dict);
        List<DictVO> list = dictMapper.selectDict(dict);
        log.debug("查询字典信息:list={}", list);
        return list;
    }

    public void saveDict(DictSaveParam dictSaveParam) {
        log.debug("保存字典业务参数:{}", dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam, dict);
        if (dict.getId() == null) {
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        } else {
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }
    public void delete(Long id) {
        log.debug("删除字典业务参数:{}", id);
        dictMapper.delete(id);
        dictOptionService.deletebyDictId(id);
    }
}